package com.microservice.consumer.movie.ctrl;

import com.microservice.consumer.movie.service.UserFeignInterface;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.netflix.feign.FeignClientsConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.microservice.consumer.movie.bean.User;

@RestController
@Log
@Import(FeignClientsConfiguration.class)
public class MovieConsumerCtrl {
	@Autowired
	private UserFeignInterface userFeignInterface;
	@Autowired
	private LoadBalancerClient loadBalancerClient;

//	@HystrixCommand(fallbackMethod = "findInfoFallBack",commandProperties = {
//			@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "60000"),
//			@HystrixProperty(name="metrics.rollingStats.timeInMilliseconds",value = "10000")
//	}, threadPoolProperties = {
//			@HystrixProperty(name="coreSize",value="1"),
//			@HystrixProperty(name="maxQueueSize",value="10")
//	})
	@GetMapping("/user/{id}")
	public User findInfo(@PathVariable Long id){
		System.err.println("Consumer-Movie:"+id);
		User user = userFeignInterface.findById(id);
		log.warning(user.toString());
		return user;
	}
	public User findInfoFallBack(Long id){
		User user = new User();
		user.setId(id);
		user.setName("断路器：异常用户");
		log.info("断路器-用户查询");
		return user;
	}
	@GetMapping("/logInstance")
	public void logInstance(){
		ServiceInstance serviceInstance = this.loadBalancerClient.choose("provider-user");
		log.info(serviceInstance.getServiceId()+":"+serviceInstance.getHost()+":"+serviceInstance.getPort());
	}
}
